import time

from selenium.webdriver.common.by import By
from DBUtils import DBUtils


class AddCardOperation:
    def addcard(self, driver, username, password, bankname, cardtype, cardnum):
        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
        # time.sleep(1)
        # 点击登录按钮
        driver.find_element(By.XPATH, '//button[@id="login_btn"]').click()
        time.sleep(1)
        # 点击个人中心
        driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[4]/a").click()
        time.sleep(1)
        # 点击银行卡管理
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]').click()
        # 点击新增按钮
        driver.find_element(By.XPATH, '//button[@id="bankCard_add_modal_btn"]').click()
        time.sleep(1)
        # 输入银行名称
        driver.find_element(By.XPATH, '//input[@id="cardBank_add_input"]').send_keys(bankname)
        # 选择银行卡类型
        if cardtype == '1':
            driver.find_element(By.XPATH,
                                '//*[@id="bankCardAddModal"]/div/div/div[2]/form/div[2]/div[1]/div/label[1]').click()
        else:
            driver.find_element(By.XPATH,
                                '//*[@id="bankCardAddModal"]/div/div/div[2]/form/div[2]/div[1]/div/label[2]').click()
        # 输入卡号
        driver.find_element(By.XPATH, '//input[@id="cardNum_add_input"]').send_keys(cardnum)
        # 点击保存按钮
        driver.find_element(By.XPATH, '//*[@id="bankCard_save_btn"]').click()
        time.sleep(1)

    def addcard_result(self, cardnum, listlen):
        dbutils = DBUtils()
        result = dbutils.select('select cardNum from bankcard')
        if len(result) > listlen:
            if (cardnum,) in result:
                return 1
        else:
            return 0
